from client import Client


def add(o,region,name):
    o['region']=region
    o['region_name']=name
    return o


if __name__ == '__main__':
    '''
        迁移合并数据 
    '''
    client = Client()
    regions = client.get_collection('regions')
    comms = client.get_collection('communities')

    regs = [r['region'] for r in regions.find({},{'region':1})]
    reg_names = [r['name'] for r in regions.find({},{'name':1})]
    print(regs)
    # for r in regs:
    #     condition = {'region':r}
    #     com = comms.find_one(condition)
    #
    #     dst = regions.find_one(condition)
    #     dst['communities'] = com['communities']
    #     regions.update_one(condition,{'$set':dst})
    #     print(dst)
    comms.drop()

    for r,n in zip(regs,reg_names):
        communities = regions.find({'region':r})[0]['communities']
        for c in communities:
            add(c,r,n)

        comms.insert_many(communities)